<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" label-width="68px">
          <el-form-item label="电梯名称" prop="elevatorName">
            <el-input v-model="queryParams.elevatorName" clearable placeholder="请输入电梯名称" style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['iot:annualPlan:remove']" :disabled="multiple" icon="Delete" plain type="danger" @click="handleDelete()"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['iot:annualPlan:export']" icon="Download" plain type="warning" @click="handleExport">导出 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['iot:annualPlan:edit']" icon="Plus" plain type="success" @click="generate()"> 一键生成 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="annualPlanList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="电梯名称" prop="elevatorName" />
        <el-table-column align="center" label="注册码" prop="registeredCode" />
        <el-table-column align="center" label="安装日期" prop="installationDate" />
        <el-table-column align="center" label="年检日期" prop="annualDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.annualDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="限速器年检日期" prop="speedGovernor" />
        <el-table-column align="center" label="备注" prop="remark" />
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template #default="scope">
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['iot:annualPlan:remove']" icon="Delete" link type="primary" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script lang="ts" name="AnnualPlan" setup>
import { delAnnualPlan, generateAnnualPlan, listAnnualPlan } from "@/api/iot/annualPlan";
import { AnnualPlanForm, AnnualPlanQuery, AnnualPlanVO } from "@/api/iot/annualPlan/types";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const annualPlanList = ref<AnnualPlanVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const annualPlanFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ""
});

const initFormData: AnnualPlanForm = {
  planId: undefined,
  elevatorId: undefined,
  elevatorName: undefined,
  registeredCode: undefined,
  installationDate: undefined,
  annualDate: undefined,
  speedGovernor: undefined,
  remark: undefined
};
const data = reactive<PageData<AnnualPlanForm, AnnualPlanQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    elevatorId: undefined,
    elevatorName: undefined,
    registeredCode: undefined,
    installationDate: undefined,
    annualDate: undefined,
    speedGovernor: undefined,
    params: {}
  },
  rules: {
    planId: [
      { required: true, message: "计划ID不能为空", trigger: "blur" }
    ],
    elevatorId: [
      { required: true, message: "关联电梯ID不能为空", trigger: "blur" }
    ],
    elevatorName: [
      { required: true, message: "电梯名称不能为空", trigger: "blur" }
    ],
    registeredCode: [
      { required: true, message: "注册码不能为空", trigger: "blur" }
    ],
    installationDate: [
      { required: true, message: "安装日期不能为空", trigger: "blur" }
    ],
    annualDate: [
      { required: true, message: "年检日期不能为空", trigger: "blur" }
    ],
    speedGovernor: [
      { required: true, message: "限速器年检日期不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询电梯年检计划列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAnnualPlan(queryParams.value);
  annualPlanList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  annualPlanFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: AnnualPlanVO[]) => {
  ids.value = selection.map(item => item.planId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 删除按钮操作 */
const handleDelete = async (row?: AnnualPlanVO) => {
  const _planIds = row?.planId || ids.value;
  await proxy?.$modal.confirm("是否确认删除电梯年检计划编号为\"" + _planIds + "\"的数据项？").finally(() => loading.value = false);
  await delAnnualPlan(_planIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download("iot/annualPlan/export", {
    ...queryParams.value
  }, `annualPlan_${new Date().getTime()}.xlsx`);
};
/** 一键生成 */
const generate = async () => {
  await proxy?.$modal.confirm("是否一键生成年检计划？").finally(() => loading.value = false);
  await generateAnnualPlan();
  await proxy?.$modal.msgSuccess("生成成功");
  await getList();
};
onMounted(() => {
  getList();
});
</script>
